package com.opgea.crm.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import org.springframework.stereotype.Repository;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.domain.entities.Lead;
import org.apache.commons.lang.StringUtils;


@Repository
public class LeadRepoImpl implements LeadRepo{

	@PersistenceContext
	private EntityManager em;
	
	public void create(Lead lead) {
		em.persist(lead);
	}

	@Override
	public void update(Lead lead) {
	    if (em.find(Lead.class, lead.getId()) == null) {
	        throw new IllegalArgumentException("Unknown lead id: " + lead.getId());
	      }
		Lead managedLead = em.merge(lead);
		//em.refresh(lead);
	}	
	
	public void remove(long id) {
		// TODO Auto-generated method stub
	}

	public Lead find(long id) {
		return em.find(Lead.class, id);	
	}

	public List<Lead> findAll() {
		String query = "SELECT entObj FROM Lead entObj";
		List<Lead> leadList = em.createQuery(query, Lead.class).getResultList();
		return leadList;
	}

	@Override
	public List<Lead> findAllByCompanyId(long companyId) {
		String query = "SELECT l FROM Lead l WHERE l.company.id = "+companyId;
		return em.createQuery(query, Lead.class).getResultList();
	}

	@Override
	public List<Lead> findDynamically(String criteria, String value, ApplicationConstant.LeadStatusQualifier leadStatusFlag) {
		
		StringBuffer buffer=new StringBuffer("SELECT l FROM Lead l");
		String cond=" WHERE ";
		if(Integer.valueOf(criteria)==ApplicationConstant.LeadSearchCriteriaQualifier.FirstName.ordinal()){
			buffer.append(cond);
			buffer.append(" l.firstName like :paramVal");
			cond = " AND ";
			}
		
		if(Integer.valueOf(criteria)==ApplicationConstant.LeadSearchCriteriaQualifier.LastName.ordinal()){
			buffer.append(cond);
			buffer.append(" l.lastName like :paramVal");
			cond = " AND ";
		}
		
		if(Integer.valueOf(criteria)==ApplicationConstant.LeadSearchCriteriaQualifier.Email.ordinal()){
			buffer.append(cond);
			buffer.append(" l.leademail.address like :paramVal");
			cond = " AND ";
		}		
		
		if(Integer.valueOf(criteria)==ApplicationConstant.LeadSearchCriteriaQualifier.CompanyName.ordinal()){
			buffer.append(cond);
			buffer.append(" l.company.name like :paramVal");
			cond = " AND ";
		}
		buffer.append(cond);
		buffer.append(" l.leadStatus = :status");
		
		Query query=em.createQuery(buffer.toString());
		if(StringUtils.isNotEmpty(value))
		query.setParameter("paramVal", value+'%');
		query.setParameter("status", leadStatusFlag);
		System.out.println(buffer.toString());
		return (List<Lead>)query.getResultList();
	}
	
	@Override
	public List<Lead> findDynamically(String criteria, String value) {
		
		StringBuffer buffer=new StringBuffer("SELECT l FROM Lead l");
		String cond=" WHERE ";
		if(Integer.valueOf(criteria)==ApplicationConstant.LeadSearchCriteriaQualifier.FirstName.ordinal()){
			buffer.append(cond);
			buffer.append(" l.firstName like :paramVal");
			cond = " AND ";
			}
		
		if(Integer.valueOf(criteria)==ApplicationConstant.LeadSearchCriteriaQualifier.LastName.ordinal()){
			buffer.append(cond);
			buffer.append(" l.lastName like :paramVal");
			cond = " AND ";
		}
		
		if(Integer.valueOf(criteria)==ApplicationConstant.LeadSearchCriteriaQualifier.Email.ordinal()){
			buffer.append(cond);
			buffer.append(" l.leademail.address like :paramVal");
			cond = " AND ";
		}		
		
		if(Integer.valueOf(criteria)==ApplicationConstant.LeadSearchCriteriaQualifier.CompanyName.ordinal()){
			buffer.append(cond);
			buffer.append(" l.company.name like :paramVal");
			cond = " AND ";
		}
		
		Query query=em.createQuery(buffer.toString());
		if(StringUtils.isNotEmpty(value))
		query.setParameter("paramVal", value+'%');
		System.out.println(buffer.toString());
		return (List<Lead>)query.getResultList();
	}

	
	public List<Lead> findLeadList(Long assignedToId,Long gradeId,Long stageId) {
		StringBuffer buffer = new StringBuffer(" SELECT l FROM Lead l ");
		String cond = " WHERE ";
		if(assignedToId != null)
		{
			buffer.append(cond);
			buffer.append(" l.leadprofile.employee.id = :assignTo ");
			cond = " AND ";
		}
		if(gradeId != null &&  (!gradeId.equals("")) && (gradeId!=0))
		{
			buffer.append(cond);
			buffer.append(" l.leadprofile.grade.id = :grade ");
			cond = " AND ";
		}
		if(stageId != null  && (!stageId.equals("")) && (stageId!=0))
		{
			buffer.append(cond);
			buffer.append(" l.leadprofile.stage.id = :stage ");
			cond = " AND ";
		}
		
		Query query = em.createQuery(buffer.toString());
		
		if(assignedToId != null)
		{
			query.setParameter("assignTo",assignedToId);
		}
		if(gradeId != null  && (!gradeId.equals("")) && (gradeId!=0))
		{
			query.setParameter("grade", gradeId);
		}
		if(stageId != null  && (!stageId.equals("")) && (stageId!=0))
		{
			query.setParameter("stage", stageId);
		}
		
		List<Lead> result = query.getResultList();
		System.out.println("LeadList :" +result.size());
		return result;
	}
	
	
}
